package com.class5.springboot.frame.repo.real;

import java.lang.reflect.Method;

import lombok.extern.slf4j.Slf4j;
@Slf4j
public class FieldMapper {
    //属性名
    private String name;
    //属性类型
    private Class<?> type;
    //列名
    private String colName;
    //标签
    private String label;
    //是否为id
    private boolean identity = false;
    //是否需要使用数据库自增
    private boolean autoIncrement = false;
    //使用NormalRepository保存时，是否需要由程序生成唯一序列值
    private boolean autoIncByRepo = false;
    //是否为版本字段
    private boolean version = false;


    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getColName() {
        return colName;
    }
    public void setColName(String colName) {
        this.colName = colName;
    }
    public String getLabel() {
        return label;
    }
    public void setLabel(String label) {
        this.label = label;
    }
    @Override
    public int hashCode() {
        return name.hashCode();
    }
    @Override
    public boolean equals(Object obj) {
        return ((FieldMapper)obj).getName().equals(name);
    }
    public boolean isIdentity() {
        return identity;
    }
    public void setIdentity(boolean identity) {
        this.identity = identity;
    }
    public boolean isVersion() {
        return version;
    }
    public void setVersion(boolean version) {
        this.version = version;
    }
    @SuppressWarnings("unchecked")
    public <T> T getValue(Object entity) {
        try {
            Method method = entity.getClass().getMethod("get" + name.substring(0, 1).toUpperCase()+name.substring(1), new Class<?>[]{});
            return (T)method.invoke(entity, new Object[]{});
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    public void setValue(Object entity, Object propValue){
        String methodName = "set" + name.substring(0, 1).toUpperCase()+name.substring(1);
        try {
            Method method = entity.getClass().getMethod(methodName, new Class<?>[]{type});
            method.invoke(entity, new Object[]{propValue});
        } catch (Exception e) {
            log.debug("Execute {} with param {}({}) error", methodName, propValue, (propValue!=null?propValue.getClass().getName():""));
            throw new RuntimeException(e);
        }
    }

    public Class<?> getType() {
        return type;
    }
    public void setType(Class<?> type) {
        this.type = type;
    }
    @Override
    public String toString() {
        return "FieldMapper [name=" + name + ", type=" + type + ", colName=" + colName + ", label=" + label
                + ", identity=" + identity + ", autoIncrement=" + autoIncrement + ", autoIncByRepo=" + autoIncByRepo
                + ", version=" + version + "]";
    }
    public boolean isAutoIncrement() {
        return autoIncrement;
    }
    public void setAutoIncrement(boolean autoIncrement) {
        this.autoIncrement = autoIncrement;
    }
    public boolean isAutoIncByRepo() {
        return autoIncByRepo;
    }
    public void setAutoIncByRepo(boolean autoIncByRepo) {
        this.autoIncByRepo = autoIncByRepo;
    }

}
